package cn.tedu.hotel.serve.service.impl;


import cn.tedu.hotel.serve.ex.ServiceException;
import cn.tedu.hotel.serve.mapper.HotelMapper;
import cn.tedu.hotel.serve.mapper.HotelScoreMapper;
import cn.tedu.hotel.serve.mapper.UserMapper;
import cn.tedu.hotel.serve.pojo.dto.HotelScoreDTO;
import cn.tedu.hotel.serve.pojo.entity.HotelScore;
import cn.tedu.hotel.serve.pojo.vo.HotelScoreVO;
import cn.tedu.hotel.serve.pojo.vo.HotelStandardVO;
import cn.tedu.hotel.serve.pojo.vo.UserStandardVO;
import cn.tedu.hotel.serve.service.IHotelScoreService;
import cn.tedu.hotel.serve.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;


@Slf4j
@Service
public class HotelScoreServiceImpl implements IHotelScoreService {

    @Autowired
    HotelScoreMapper hotelScoreMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    HotelMapper hotelMapper;

    @Override
    public void addHotelScore(HotelScoreDTO hotelScoreDTO) {
        log.debug("开始处理【前台用户对酒店进行评分】的业务，参数：{}",hotelScoreDTO);
        Long userId=hotelScoreDTO.getUserId();
        UserStandardVO userStandardVO = userMapper.getStandardById(userId);
        if (userStandardVO==null){
            String message = "用户不存在请先登录！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        Long hotelId= hotelScoreDTO.getHotelId();
        HotelStandardVO standardVO = hotelMapper.getHotelById(hotelId);
        if (standardVO==null){
            String message = "酒店不存在，请稍后重试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        HotelScoreVO hotelScoreVO =
                hotelScoreMapper.getHotelScoreByUserIdAndHotelId(userId, hotelId);
        if (hotelScoreVO!=null){
            log.debug("后台数据库中存在用户对酒店：{}的评分数据",hotelScoreDTO.getHotelName());
            log.debug("开始处理【前台用户对酒店评分进行】的业务，参数：{}",hotelScoreDTO.getScore());
            BigDecimal score=hotelScoreDTO.getScore();
            int rows = hotelScoreMapper.updateHotelScoreByUserIdAndHotelId(score, userId, hotelId);
            if (rows!=1){
                String message = "服务器繁忙，请稍后重试！";
                log.warn(message);
                throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
            }
            return;
        }
        log.debug("后台用户：{}没有对该酒店{}进行过评分，增加评分数据:{}"
                ,hotelScoreDTO.getUsername(),hotelScoreDTO.getHotelName(),hotelScoreDTO.getScore());
        HotelScore hotelScore=new HotelScore();
        BeanUtils.copyProperties(hotelScoreDTO,hotelScore);
        int rows = hotelScoreMapper.addHotelScore(hotelScore);
        if (rows!=1){
            String message = "服务器繁忙，请稍后重试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }
    }

    @Override
    public HotelScoreVO getHotelScoreByUserIdAndHotelId(Long userId, Long hotelId) {
        log.debug("开始处理【前台根据酒店ID：{}，用户ID：{}，查询用户对酒店的评分】的业务，参数：{}",
                hotelId,userId);
        UserStandardVO user = userMapper.getStandardById(userId);
        if (user==null){
            String message = "用户不存在请先登录！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        HotelStandardVO hotel = hotelMapper.getHotelById(hotelId);
        if (hotel==null){
            String message = "酒店不存在，请稍后重试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        HotelScoreVO hotelScoreVO = hotelScoreMapper.getHotelScoreByUserIdAndHotelId(userId, hotelId);
        return hotelScoreVO;
    }

    @Override
    public List<HotelScoreVO> getAdminUserScoreList() {
        log.debug("开始处理【后台查询评分列表】的业务");
        List<HotelScoreVO> list = hotelScoreMapper.getAdminUserScoreList();
        log.debug("开始处理【后台查询评分列表】的业务,数据：{}",list);
        return list;
    }

    @Override
    public void deleteScoreById(Long id) {
        log.debug("开始处理【后台根据id:{}删除表中的数据】的业务",id);
        HotelScoreVO hotelScoreVO = hotelScoreMapper.getScoreById(id);
        if (hotelScoreVO==null){
            String message = "分数数据不存在，请稍后重试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        int rows = hotelScoreMapper.deleteScoreById(id);
        if (rows!=1){
            String message = "酒店不存在，请稍后重试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
    }
}
